clear
clear matrix
clear mata
set matsize 4000
set maxvar 10000

*Note: first change working directory replication package folder

run "scripts/programs/program_distlag.do"

/*********************************************************************************/
*Set program parameters
local radius = 20
local angle = 15
local length = 1
/**********************************************************************************/

/***************************** ALMON LAG TABLE - Assessors *************************/

/************************************** HU Data ***********************************/

use "data/`radius'-by-`length'km_`angle'deg/data_centroid.dta", clear
merge m:1 firenum using ///
	"data/`radius'-by-`length'km_`angle'deg/sample_assessors.dta", ///
	nogen

local controls lnt tmissing lnint wind_diff majorroad
local lags any_res count_res avgval_res val_res habpolytot wsimptot campground wilderness
local costlags tritot roadpcttot

label var roadpcttot "Area $< 0.5$ km from road"
label var tmissing "$\Delta T$ missing"
label var lnt "Ln($\Delta T$ + 1)"

/************************************** Run regressions ***********************************/

local cond `"complexfire != 1 & year > 2011 & sample == 1"'

distlag y `controls'  if `cond', ///
	method(almon) params(2,5) lagvars(`lags') costlags(`costlags') ///
	burnt(centroid) tmethod(log) fe(firenum) cluster(firenum)

summ number, meanonly
local controls lnt tmissing lnint wind_diff majorroad

local num = `r(max)'
foreach var in `controls' {
	local n = _N + 1
	set obs `n'
	replace var = "`var'" if _n == `n'
	replace beta = _b[`var'] if _n == `n'
	replace se = _se[`var'] if _n == `n'
	replace lag = 0 if _n == `n'
	local num = `num' + 1
	replace number = `num' if _n == `n'
	}

gen pval = 1-normal(abs(b/se))
gen stars = cond(pval < .01,"**",cond(pval < .05,"*",cond(pval < .1,"+","")))
tostring b, replace format(%9.2gc) force
tostring se, replace format(%9.2gc) force
replace b = b + stars
replace se = "(" + se + ")"
drop pval stars ul ll method


reshape wide b se, i(var) j(lag)


foreach var of varlist beta0-se5 {
	local leadnum = substr("`var'",-1,.)
	if substr("`var'",1,1) == "b" rename `var' F`leadnum'1
	else if substr("`var'",1,1) == "s" rename `var' F`leadnum'2
	}

reshape long F0 F1 F2 F3 F4 F5, i(var) j(row)

replace var = "" if row == 2
replace var = "(No. res. props. $> 0$)" if var == "any_res"
replace var = "No. res. props." if var == "count_res"
replace var = "Avg. value res. props." if var == "avgval_res"
replace var = "Total value res. props." if var == "val_res"
replace var = "Topographic ruggedness index" if var == "tritot"
replace var = "Area $<0.5\%$ km from road" if var == "roadpcttot"
replace var = "Ln($\Delta + 1$)" if var == "lnt"
replace var = "Ln(Intensity)" if var == "lnint"
replace var = "Contains major road" if var == "majorroad"
replace var = "$\Delta T$ missing" if var == "tmissing"
replace var = "TES habitat" if var == "habpolytot"
replace var = "Watershed importance" if var == "wsimptot"
replace var = "Wind difference" if var =="wind_diff"
replace var = "Wilderness" if var == "wilderness"
replace var = "Campground" if var == "campground"

sort number row
drop row number

local n = _N + 3
set obs `n'
replace var = "Fire fixed effects" if _n == `n'-2
replace F0 = "Yes" if _n == `n'-2
replace var = "Number obs." if _n == `n'-1
replace F0 = "`e(N)'" if _n == `n'-1
replace var = "Number fires" if _n == `n'
replace F0 = "`e(N_clust)'" if _n == `n'

replace F5 = F5 + "\\"

local dir "Results/Appendix_Assessor_Almon/"
capture mkdir `dir'
export delimited using "`dir'table_contents.tex", delim("&") novarnames replace


/***************************** ALMON LAG TABLE - Census *************************/

/************************************** HU Data ***********************************/

use "data/`radius'-by-`length'km_`angle'deg/data_centroid.dta", clear
merge m:1 firenum using ///
	"data/`radius'-by-`length'km_`angle'deg/sample_census.dta", ///
	nogen

local controls lnt tmissing lnint wind_diff majorroad
local lags anyhu hu inc_pcincome huxpcinc habpolytot wsimptot campground wilderness
local costlags tritot roadpcttot

label var roadpcttot "Area $< 0.5$ km from road"
label var tmissing "$\Delta T$ missing"
label var lnt "Ln($\Delta T$ + 1)"

/************************************** Run regressions ***********************************/

local cond `"complexfire != 1 & sample == 1"'

distlag y `controls'  if `cond', ///
	method(almon) params(2,5) lagvars(`lags') costlags(`costlags') ///
	burnt(centroid) tmethod(log) fe(firenum) cluster(firenum)

summ number, meanonly
local num = `r(max)'
foreach var in `controls' {
	local n = _N + 1
	set obs `n'
	replace var = "`var'" if _n == `n'
	replace beta = _b[`var'] if _n == `n'
	replace se = _se[`var'] if _n == `n'
	replace lag = 0 if _n == `n'
	local num = `num' + 1
	replace number = `num' if _n == `n'
	}

gen pval = 1-normal(abs(b/se))
gen stars = cond(pval < .01,"**",cond(pval < .05,"*",cond(pval < .1,"+","")))
tostring b, replace format(%9.2gc) force
tostring se, replace format(%9.2gc) force
replace b = b + stars
replace se = "(" + se + ")"
drop pval stars ul ll method

reshape wide b se, i(var) j(lag)

foreach var of varlist beta0-se5 {
	local leadnum = substr("`var'",-1,.)
	if substr("`var'",1,1) == "b" rename `var' F`leadnum'1
	else if substr("`var'",1,1) == "s" rename `var' F`leadnum'2
	}

reshape long F0 F1 F2 F3 F4 F5, i(var) j(row)

replace var = "" if row == 2
replace var = "(No. HU $> 0$)" if var == "anyhu"
replace var = "No. HU" if var == "hu"
replace var = "Per cap. income" if var == "inc_pcincome"
replace var = "No. HU $\times$ per cap. inc." if var == "huxpcinc"
replace var = "Topographic ruggedness index" if var == "tritot"
replace var = "Area $<0.5\%$ km from road" if var == "roadpcttot"
replace var = "Ln($\Delta + 1$)" if var == "lnt"
replace var = "Ln(Intensity)" if var == "lnint"
replace var = "Contains major road" if var == "majorroad"
replace var = "$\Delta T$ missing" if var == "tmissing"
replace var = "TES habitat" if var == "habpolytot"
replace var = "Watershed importance" if var == "wsimptot"
replace var = "Wind difference" if var =="wind_diff"
replace var = "Wilderness" if var == "wilderness"
replace var = "Campground" if var == "campground"
sort number row
drop row number

local n = _N + 3
set obs `n'
replace var = "Fire fixed effects" if _n == `n'-2
replace F0 = "Yes" if _n == `n'-2
replace var = "Number obs." if _n == `n'-1
replace F0 = "`e(N)'" if _n == `n'-1
replace var = "Number fires" if _n == `n'
replace F0 = "`e(N_clust)'" if _n == `n'

replace F5 = F5 + "\\"

local dir "Results/Appendix_Census_Almon/"
capture mkdir `dir'
export delimited using "`dir'table_contents.tex", delim("&") novarnames replace
